const query=require('../models');


module.exports={
  //登录
  login: (req,res) => {
    console.log('接收到用户请求：',req.body);
    const username = req.body.username;
    const password = req.body.password;   
    const sql = 'SELECT user_id userId,user_name username,user_pass pasdword,is_del isDel ,is_loginPermission islogin FROM admin_users  JOIN quthority USING (quthority_id) WHERE user_name=?&&user_pass=? &&is_del=1 ;';
    query(sql, [username, password])
      .then((result)=>{
        console.log('登录请求成功', result)
        if (result.length===0){
          res.json({
            code: '1001',
            msg: '账号或者密码错误'
          });
        }
        console.log('登录权限：', result[0].islogin)
        if (result[0].islogin == 0){
            console.log('当前账户没有登陆权限，请联系管理员处理');
            res.json({
              code: '1002',
              msg: '当前账户没有登陆权限，请联系管理员处理'
            });
        }else{
          req.session.sign = true; //权限设置 --应该在登录成功之后设置
          console.log('登录成功查看res：', req.session.sign)
          console.log(result[0])
          res.json({
            code:'200',
            msg:'登录成功',
            data: result[0]
          });
          }
      })
    .catch((err)=>{
      console.log('登录请求失败，错误',err);
    }
    )
  },

  //注册
  occupy: (req, res)=> {
    console.log('接收到用户请求：', req.body);
    //获取输入框的值
    let username = req.body.username;
    let userpass = req.body.userpass;
    let useremail = req.body.useremail;
    //将账号和邮箱与数据中的数据比对，因为账号和邮箱具有唯一性，所有只要有一个存在，就返回被占用，如果数据库里没有就将账号邮箱和密码添加到数据库里，数据库会自动生成一个id
    const sql = 'SELECT user_name AS username,user_email AS useremail FROM admin_users WHERE user_name=?||user_email=?';
    query(sql, [username, useremail])
      .then((data)=>{
        console.log('第一层')
        if (data.length === 0) {

          console.log('第二层')
          //数据库中返回为空，就将数据写入数据库
          const sql = 'INSERT INTO admin_users (user_name,user_pass,user_email) VALUES (?,?,?);';
          query(sql, [username, userpass, useremail])
            .then((median)=>{
              console.log('第三层')
              //获取新增用户的id
              const sql = 'SELECT user_id AS userId  FROM admin_users WHERE user_name=?';
              query(sql, [username])
                .then((result)=>{
                  console.log('注册后的数据：', result)
                  req.session.sign = true; //权限设置 --应该在登录成功之后设置
                  res.json({
                      code: '200',
                      msg: '数据已成功添加到数据库',
                      data: {
                        userId: result[0].userId
                      }
                    })
                })
            })
            .catch((err)=>{
              console.log(err)
            })
        }
      })
    .catch((res)=>{
        res.json({
          code: '1002',
          msg: '邮箱或账号已被占用',
        })
      })
    },
  //登录之后获取登录用户的全部信息
  getInformation: (req,res) =>{
    console.log('接收到用户请求：', req.query);
    const loginId = req.query.loginId;
    console.log('接收到的ID：',loginId)
    const sql = 'SELECT avator, is_teacher , is_student,is_course, is_books,is_department,is_administrator,is_application FROM admin_users LEFT JOIN `quthority` USING (quthority_id)  WHERE user_id=?';

    query(sql, [loginId])
     .then( (data)=> {
        if (data.length === 0) {
          res.json({
            code: '1001',
            msg: '查询错误'
          });
        } else {
          res.json({
            code: '200',
            msg: '请求成功',
            data: data[0]
          })
        }
      })
      .catch((error)=>{
        console.log()
      })
  }
}